package com.hisoft.utility;

import org.apache.log4j.Logger;

import com.hisoft.constant.Xpath;
import com.hisoft.entity.AccountEntity;
import com.hisoft.entity.DeviceEntity;
import com.hisoft.entity.Environment;
import com.hisoft.entity.VariableMap;
import com.hisoft.exception.XpathNotFoundException;
import com.hisoft.type.AccountType;
import com.hisoft.xml.Dom4JXMLHelper;

/**
 * test case对象化时数据进行初始化，
 * <b> </b>
 * 
 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-7
 */
public class DataProviderTool {
	static Logger logger = Logger.getLogger(DataProviderTool.class);

	/**
	 * 
	 * <b> 根据字符串获得数据</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-24
	 */
	public static String getData(String str) {
		String returnStr = "";
		if (str.contains("xml(")) {
			if (str.contains(").xpath.")) {
				returnStr = xpath(getName(str, ").xpath."), getXMLPath(str));
			} else if (str.contains(").message.")) {
				returnStr = message(getName(str, ").message."), getXMLPath(str));
			} else if (str.contains(").js.")) {
				returnStr = js(getName(str, ").js."), getXMLPath(str));
			} else if (str.contains(").image.")) {
				returnStr = image(getName(str, ").image."), getXMLPath(str));
			} else if (str.contains(").color.")) {
				returnStr = color(getName(str, ").color."), getXMLPath(str));
			} else if (str.contains(").pagePath.")) {
				returnStr = pagePath(getName(str, ").pagePath."), getXMLPath(str));
			} else if (str.contains(").font.")) {
				returnStr = font(getName(str, ").font."), getXMLPath(str));
			} else {
				returnStr = str;
			}
			// xpath(login).TitleBar
		} else if (str.contains("xpath(")) {
			returnStr = xpath(str.split("\\.")[1], getXMLPath(str));
		} else if (str.contains("url(")) {
			returnStr = pagePath(str.split("\\.")[1], getXMLPath(str));
		} else if (str.contains("js(")) {
			returnStr = js(str.split("\\.")[1], getXMLPath(str));
		} else if (str.contains("message(")) {
			returnStr = message(str.split("\\.")[1], getXMLPath(str));
		} else if (str.contains("image(")) {
			returnStr = image(str.split("\\.")[1], getXMLPath(str));
		} else if (str.contains("account(")) {
			returnStr = AccountInfo(str.substring(str.indexOf("account(") + "account(".length(), str.indexOf(").")), str);
		} else if (str.contains("email(")) {
			returnStr = getEmailInfo(str.substring(str.indexOf("email(") + "email(".length(), str.indexOf(").")), str);
		} else {
			returnStr = str;
		}
		return returnStr;
	}

	/**
	 * 
	 * 
	 * <b>获得邮件信息</b><br/>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-7-28
	 */
	private static String getEmailInfo(String name, String str) {
		String returnStr = "";
		String xpath = Xpath.EMAILINFO_XPATH.replace(Xpath.REPLACE, name) + "/";
		String xpath2 = str.split("\\.")[1];
		String language = "message[@name='" + xpath2 + "']/content[@language='" + Environment.getInstance().getLanguage() + "']";
		try {
			returnStr = Dom4JXMLHelper.readSingleNode(xpath + language, Environment.getInstance().getEmailInfoFilePath()).getText();
		} catch (XpathNotFoundException e) {
			returnStr = Dom4JXMLHelper.readSingleNode(xpath + xpath2, Environment.getInstance().getEmailInfoFilePath()).getText();
		}
		returnStr = returnStr.replace("#$firstName$#", getData("account(" + name + ").firstName")).replace("#$DNSName$#", Environment.getInstance().getBaseUrl().split("/")[2]);
		return returnStr;
	}

	/**
	 * 
	 * 从变量对象中获得变量
	 * <b></b><br/>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 上午10:05:16
	 */
	public static String getProperty(String str) {
		String returnStr = str;
		if (str == null) {
			returnStr = "";
		} else if (str.contains("property(")) {
			returnStr = getValue(str.substring(str.indexOf("property(") + "property(".length(), str.indexOf(")")));
		} else if (str.contains("${")) {
			returnStr = getValue(str.substring(str.indexOf("${") + "${".length(), str.indexOf("}")));
		}
		return returnStr;
	}
	
	/**
	 * 
	 * 从变量对象中获得变量
	 * <b></b><br/>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 上午10:05:16
	 */
	private static String getValue(String key) {
		return VariableMap.getInstance().mapGetValue(key);
	}

	/**
	 * 
	 * <b> 获得font标签的值</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-24
	 */
	private static String font(String name, String xmlPath) {
		String xpath = Xpath.FONT_XPATH.replace(Xpath.REPLACE, name);
		if (Environment.getInstance().getBrowser().toLowerCase().contains("ie")) {
			xpath += "/ie";
		} else {
			xpath += "/notIe";
		}
		return Dom4JXMLHelper.readSingleNode(xpath, xmlPath).getText();
	}

	/**
	 * 
	 * <b> 获得color标签的值</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-24
	 */
	private static String color(String name, String xmlPath) {
		String xpath = Xpath.COLOR_XPATH.replace(Xpath.REPLACE, name);
		if (Environment.getInstance().getBrowser().toLowerCase().contains("ie")) {
			xpath += "/ie";
		} else {
			xpath += "/notIe";
		}
		return Dom4JXMLHelper.readSingleNode(xpath, xmlPath).getText();
	}

	/**
	 * 
	 * <b> </b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-24
	 */
	private static String getName(String str, String afterString) {
		return str.substring(str.indexOf(afterString) + afterString.length());
	}

	/**
	 * 
	 * <b> </b> xpath(login).TitleBar获得括号里的值
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-24
	 */
	private static String getXMLPath(String str) {
		return Environment.getInstance().getPageDataPath() + (Environment.getInstance().getPageDataPath().endsWith("/") ? "" : "/") + str.substring(str.indexOf("(") + "(".length(), str.indexOf(")")) + ".xml";
	}

	/**
	 * 
	 * <b>获得xpath标签的值 </b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-24
	 */
	private static String xpath(String name, String xmlpath) {
		return Dom4JXMLHelper.readSingleNode(Xpath.XPATH_XPATH.replace(Xpath.REPLACE, name), xmlpath).getText();
	}

	/**
	 * 
	 * <b> 获得pagePath标签的值</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-24
	 */
	private static String pagePath(String name, String xmlpath) {
		return Dom4JXMLHelper.readSingleNode(Xpath.PAGEPATH_XPATH.replace(Xpath.REPLACE, name), xmlpath).getText();
	}

	/**
	 * 
	 * <b> 获得js标签的值</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-24
	 */
	private static String js(String name, String xmlpath) {
		return Dom4JXMLHelper.readSingleNode(Xpath.JS_XPATH.replace(Xpath.REPLACE, name), xmlpath).getText();
	}

	/**
	 * 
	 * <b> 获得message标签的值</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-24
	 */
	private static String message(String name, String xmlpath) {
		return Dom4JXMLHelper.readSingleNode(Xpath.MESSAGE_XPATH.replace(Xpath.REPLACE, name), xmlpath).getText();
	}

	/**
	 * 
	 * <b>获得image标签的值</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-24
	 */
	private static String image(String name, String xmlpath) {
		return Dom4JXMLHelper.readSingleNode(Xpath.IMAGE_XPATH.replace(Xpath.REPLACE, name), xmlpath).getText();
	}

	/**
	 * 
	 * <b>获得账户信息</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-24
	 */
	public static String AccountInfo(String name, String str) {

		AccountEntity account = Environment.getInstance().getAccountMap().get(name);
		if (account == null)
			throw new XpathNotFoundException("account named: " + name, "");
		String type = str.split("\\.")[1];

		/*
		 * return object with sValueType
		 */
		switch (AccountType.valueOf(type)) {
		case email:
			return account.getEmail();
		case pwd:
			return account.getPassword();
		case emailPwd:
			return account.getEmailPassword();
		case changedLink:
			return account.getEmailLink();
		case firstName:
			return account.getFirstName();
		case lastName:
			return account.getLastName();
			/*
			 * when type include device, mains will return device info
			 */
		case device:
			String[] strings = str.split("\\.");
			int deviceNo = Integer.parseInt(strings[2]) - 1;
			DeviceEntity device;
			try {
				device = account.getDeviceList().get(deviceNo);
			} catch (IndexOutOfBoundsException e) {
				logger.error("Index " + deviceNo + " device in account named: " + name);
				return "";
			}

			if (strings.length == 4)
				return getDeviceInfo(strings[3], device);
			else
				return "";
		default:
			return "";
		}
	}

	/**
	 * <b> Get device info. </b>
	 * 
	 * @param infoName
	 * @return
	 * @Author maiza.lin ( yunqi.lin@hisoft.com ) Create on 2011-2-16
	 */
	public static String getDeviceInfo(String infoName, DeviceEntity device) {

		switch (DeviceInfo.valueOf(infoName)) {
		case connectionType:
			return device.getConnectType();
		case deviceName:
			return device.getDeviceName();
		case mac:
			return device.getMac();
		case modelName:
			return device.getModelName();
		case passwd:
			return device.getPassWord();
		case sn:
			return device.getSn();
		case userName:
			return device.getUserName();
		case fw:
			return device.getFw();
		default:
			return "";
		}
	}

	enum DeviceInfo {
		deviceName, modelName, connectionType, sn, mac, userName, passwd, state, fw
	}

}
